using System;

namespace TTEngine.TTMath
{
    public struct Rectangle
    {
        public float Width { get; set; }
        public float Height { get; set; }
        public float X { get; set; }
        public float Y { get; set; }

        public Rectangle(float x, float y, float width, float height)
        {

            Width = width;
            Height = height;
            X = x;
            Y = y;
        }

        public bool Intersects(Rectangle other)
        {
            return X < other.X + other.Width &&
                   X + Width > other.X &&
                   Y < other.Y + other.Height &&
                   Y + Height > other.Y;
        }

        public bool Contains(Rectangle other)
        {
            return X <= other.X &&
                   Y <= other.Y &&
                   X + Width >= other.X + other.Width &&
                   Y + Height >= other.Y + other.Height;
        }

        public float X2 => X + Width;
        public float Y2 => Y + Height;

        public static Rectangle Intersect(Rectangle a, Rectangle b)
        {
            float x1 = System.Math.Max(a.X, b.X);
            float y1 = System.Math.Max(a.Y, b.Y);
            float x2 = System.Math.Min(a.X + a.Width, b.X + b.Width);
            float y2 = System.Math.Min(a.Y + a.Height, b.Y + b.Height);

            if (x2 < x1 || y2 < y1)
            {
                return new Rectangle(0, 0, 0, 0);
            }

            return new Rectangle(x1, y1, x2 - x1, y2 - y1);
        }

        public Rectangle Intersect(Rectangle other)
        {
            return Intersect(this, other);
        }
    }
}